package org.stvd.controller.oauth;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.oauth.OauthAccessToken;
import org.stvd.service.oauth.OauthAccessTokenService;

/**
 * Oauth授权信息-控制类
 * 
 * @author houzx
 */
@Controller
@RequestMapping("/oauth/accesstoken")
public class OauthAccessTokenController extends BaseController {

    @Autowired
    private OauthAccessTokenService oauthAccessTokenService; // Oauth授权信息Service

    private String tmpURL = "/oauth/accesstoken/"; // 路径模板

    /**
     * 授权信息管理GET/POST
     * 
     * @param oauthAccessToken
     * @return
     */
    @RequestMapping(value = "manage")
    public String onPageLoad(@ModelAttribute("oauthAccessToken") OauthAccessToken oauthAccessToken) {
        QueryResult<Map<String, Object>> queryResult = oauthAccessTokenService.queryAccessTokenResult((pageNo - 1) * pageSize, pageSize, 
            oauthAccessToken.getUserName(), oauthAccessToken.getClientId(), oauthAccessToken.getGrantType());
        map.put("queryResult", queryResult);
        return tmpURL + "AccessTokenManage.html";
    }

    /**
     * 授权信息删除
     * 
     * @param tokenId
     * @return
     */
    @Log(title = "删除授权信息", businessType = BusinessType.DELETE)
    @PostMapping(value = "del")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onOauthTokenDelete(String tokenId) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        try {
            result.transferResult(oauthAccessTokenService.deleteAccessToken(tokenId));
        } catch (Exception e) {
            e.printStackTrace();
            result.setError("删除失败！<br/>失败原因：" + e.getMessage());
        }
        return result;
    }
}
